package com.hxzy.servlet;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.bean.User;
import com.hxzy.bean.UserState;
import com.hxzy.service.Impl.UserServiceImpl;
import com.hxzy.service.UserService;
import com.hxzy.util.CommonUtil;
import com.hxzy.vo.JsonResult;
import com.hxzy.vo.StatusCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@WebServlet("/doLogin")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码和返回类型
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        //得到post请求的信息
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");

        if (Objects.nonNull(userName) && Objects.nonNull(userPwd)){//如果都不为null,再判断账号,密码,状态
            //通过账号查查出用户信息在和post中的信息比较判读
            UserService service=new UserServiceImpl();
            User user = service.findByAccount(userName);
            //1判断账号
            if (Objects.isNull(user)){//如果account为null则返回信息
                notMatch(resp);//把result转化为json并返回客户端
                return;
            }
            // 2判断密码
            if (!userPwd.equals(user.getLoginPassword())){//密码不相等
                notMatch(resp);//把result转化为json并返回客户端
                return;
            }
            // 3判断状态
            if (user.getState() != UserState.NORMAL.getState()){//不是正常状态
                JsonResult result=new JsonResult();
                result.setCode(StatusCode.LOCKED.getCode());
                result.setMessage(StatusCode.LOCKED.getMessage());
                CommonUtil.back(resp, result);
                return;
            }
            //4说明通过验证,跳转到用户的首页index.jsp
            HttpSession session = req.getSession();
            session.setAttribute("user",user);//添加到session作用域中

            JsonResult result=new JsonResult();
            result.setMessage(StatusCode.SUCCESS.getMessage());
            result.setCode(StatusCode.SUCCESS.getCode());
            Map map=new HashMap();
            map.put("url","index");
            result.setData(map);
            CommonUtil.back(resp,result);//把result转化为json并返回客户端

        }else {//说明post请求的有问题
            JsonResult result=new JsonResult();
            result.setCode(StatusCode.PARAMS_ERROR.getCode());
            result.setMessage(StatusCode.PARAMS_ERROR.getMessage());
            CommonUtil.back(resp,result);
            return;
        }
    }



    /**
     * 当账号或密码不匹配时,返回信息给客户端
     * @param resp
     * @throws IOException
     */
    private void notMatch(HttpServletResponse resp) throws IOException {
        JsonResult result = new JsonResult();
        //设置返回的状态信息
        result.setCode(StatusCode.INVALID.getCode());
        result.setMessage(StatusCode.INVALID.getMessage());
        CommonUtil.back(resp, result);
    }
}
